package com.jrx.anytxn.param.controller.account;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.account.SubjectDetailInfoRes;
import com.jrx.anytxn.param.service.account.ISubjectDetailInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 科目明细表管理
 *
 * @author Qingyao Xing
 * @date 2019/9/6 13:57
 */
@Api(tags = "科目明细表管理")
@RestController
@RequestMapping("param/subjectDetail")
public class SubjectDetailInfoController {

    private static final Logger logger = LoggerFactory.getLogger(SubjectDetailInfoController.class);
    @Autowired
    private ISubjectDetailInfoService subjectDetailInfoService;

    @Resource
    private InfoService infoService;

    @ApiOperation("分页查询科目明细信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<SubjectDetailInfoRes>> getAllByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize, @RequestParam("beginDate") String beginDate,@RequestParam("endDate") String endDate,@RequestParam("subjectId") String subjectId) {
        TxnRespResult<TxnPage<SubjectDetailInfoRes>> result = new TxnRespResult<>();
        try {
            result.getSuccess(subjectDetailInfoService.getPage(pageNum, pageSize,String.valueOf(infoService.getUser().getProjectId()),beginDate,endDate,subjectId));
        } catch (TxnException e) {
            logger.error("分页查询科目明细信息异常:{}", e.getErrMsg());
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("通过主键查询科目明细信息")
    @GetMapping("/{id}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)})
    public TxnRespResult<SubjectDetailInfoRes> getModalByPrimaryKey(@PathVariable(value = "id") Long id,@PathVariable String tenantId) {
        TxnRespResult<SubjectDetailInfoRes> result = new TxnRespResult<>();
        try {
            result.getSuccess(subjectDetailInfoService.getModalByIdAndTenId(id,tenantId));
        } catch (TxnException e) {
            logger.error("通过主键查询科目明细信息:{}", e.getErrMsg());
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

}
